﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using YJM.Modbus.Client.Models;

namespace YJM.Modbus.Client.Util
{
    public class ModbusConfigureHelper
    {
        public static List<ModbusDeviceProperties> readConfigure()
        {
            List<ModbusDeviceProperties> modbusDevicePropertiesList = new List<ModbusDeviceProperties>();
            //读取xml文件
            XElement xe = XElement.Load(@"./modbus.xml");
            foreach (XElement server in xe.Descendants("server"))
            {
                //初始化
                ModbusDeviceProperties mdp = new ModbusDeviceProperties();
                mdp.Signals = new List<ModbusSignal>();
                //读取属性值
                mdp.Ip = server.Element("ip").Value;
                mdp.Port = server.Element("port").Value;
                mdp.Name = server.Element("name").Value;
                foreach (XElement signal in (server.Descendants("signal")))
                {
                    ModbusSignal ms = new ModbusSignal();
                    //解析siganl节点的内容
                    //ex--1;截流组件上流压力;03;short;pressure;0.01
                    string[] ss = signal.Value.Split(';');
                    ms.Addr = int.Parse(ss[0]);
                    ms.Name = ss[1];
                    ms.Code = byte.Parse(ss[2]);
                    ms.DataType = ss[3];
                    ms.SignalType = ss[4];
                    ms.Factor = float.Parse(ss[5]);
                    //添加信号
                    mdp.Signals.Add(ms);
                }
                modbusDevicePropertiesList.Add(mdp);
            }
            return modbusDevicePropertiesList;
        }
    }
}
